﻿using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;

public class DataBattlePlayer
{
    //state:CardType
    public DataBattlePlayer(string userid, string name, int card_type, int support_type)
    {
        _userid = userid;
        _userName = name;
        _cardType = (CardType)card_type;
        _supportType = (SupportType)support_type;
        //SetDiceType(content);
        //_isExitBattle = false;
    }
    private const int _diceNum = 5;

    private int _index;
    private string _userid;
    private string _userName;//用户名（和密码一起登录）
    private CardType _cardType = CardType.Invalid;
    private SupportType _supportType = SupportType.Invalid;

    public void SetIndex(int index)
    {
        _index = index;
    }
    public int GetIndex()
    {
        return _index;
    }

    public string GetUserId()
    {
        return _userid;
    }

    public string GetIndexStr()
    {
        return "(" + (_index + 1) + ")" + (_index == 0 ? "<color=red>主</color>" : "");
    }

    public string GetUserName()
    {
        return _userName;
    }

    public void SetRoomState(string reason)
    {
        //this._isExitBattle = true;
        //this._exitBattleReason = reason;
        OnPlayerExitBattle?.Invoke(reason);
    }

    public delegate void DelegateOnCardTypeChange(CardType state);
    public DelegateOnCardTypeChange onCardTypeChange;
    public Action<string> OnPlayerExitBattle;
    public Action<SupportType> onSupportTypeChange;
    public Action<string> OnPlayerChat;
    public Action<bool> onIsDriver;

    public void SetCardType(CardType cardType)
    {
        _cardType = cardType;
        SetSupportType(SupportType.Invalid);//每次发牌，都非法
        SetIsDriver(false);//每次发牌，都非法
        onCardTypeChange?.Invoke(_cardType);
    }

    private bool _isDriver = false;
    public void SetIsDriver(bool isDriver)
    {
        _isDriver = isDriver;
        onIsDriver?.Invoke(isDriver);
    }

    public bool IsDriver()
    {
        return _isDriver;
    }
    public void SetSupportType(SupportType supportType)
    {
        _supportType = supportType;
        onSupportTypeChange?.Invoke(_supportType);
    }
    public SupportType GetSupportType()
    {
        return _supportType;
    }
    public void SetChat(string chat)
    {
        OnPlayerChat?.Invoke(chat);
    }

    public CardType GetCardType()
    {
        return _cardType;
    }

}


public enum CardType
{
     Invalid = 0,
     Merlin = 1,
     Percival = 2,
     Pengikut = 3,//可多张

     Mordred = 4,//可多张
     Morgana = 5,
     Oberon = 6,
     Assassin = 7,
}

public enum SupportType
{
    Invalid = 0,
    Support = 1,
    UnSupport = 2,

    SupportSecret = 3,
    UnSupportSecret = 4,
}

public class CardTypeHelper
{

    public static string[] NameChsList = new string[] {"待发牌中", "梅林", "派西维尔", "亚瑟的忠臣", "莫德雷德", "莫甘娜", "奥伯伦", "刺客" };
    public const string colorPrefixBlue = "<color=#0000ff>";
    public const string colorPrefixRed = "<color=#ff0000>";
    public const string colorPosfix = "</color>";
    public static string GetCardTypeNameEng(CardType cardType)
    {
        //颜色前缀
        string colorPrefix;
        if (IsBlueSide(cardType))
            colorPrefix = colorPrefixBlue;
        else
            colorPrefix = colorPrefixRed;

        return colorPrefix + cardType.ToString() + colorPosfix;
    }
    public static string GetCardTypeNameCHS(CardType cardType)
    {
        int index = (int)cardType;
        if (index < NameChsList.Length)
        {
            //颜色前缀
            string colorPrefix;
            if (IsBlueSide(cardType))
                colorPrefix = colorPrefixBlue;
            else
                colorPrefix = colorPrefixRed;

            return colorPrefix + NameChsList[index] + colorPosfix;
        }
        Debug.LogError("index:" + index + " len:" + NameChsList.Length);
        return NameChsList[0];
    }

    public static Dictionary<CardType, List<CardType>> VisibleContainers = new Dictionary<CardType, List<CardType>>() {
        { CardType.Merlin, new List<CardType>(){ CardType.Morgana, CardType.Oberon, CardType.Assassin} },
        { CardType.Percival, new List<CardType>(){ CardType.Merlin, CardType.Morgana} },
        { CardType.Pengikut, new List<CardType>()},
        { CardType.Mordred, new List<CardType>(){ CardType.Mordred, CardType.Morgana, CardType.Assassin} },
        { CardType.Morgana, new List<CardType>(){ CardType.Mordred, CardType.Assassin} },
        { CardType.Oberon, new List<CardType>() },
        { CardType.Assassin, new List<CardType>(){ CardType.Mordred, CardType.Morgana } },
    };

    public static bool IsCanSee(CardType srcCardType, CardType dstCardType)
    {
        List<CardType> list;
        if(VisibleContainers.TryGetValue(srcCardType, out list))
        {
            foreach (var item in list)
            {
                if (item == dstCardType)
                    return true;
            }
        }
        return false;
    }

    public static bool IsBlueSide(CardType cardType)
    {
        return (int)cardType < (int)CardType.Mordred;
    }

//#error 为了支持分服结构，房间号需要一个规则
    public const string blueDes = colorPrefixBlue + "蓝" + colorPosfix;
    public const string redDes = colorPrefixRed + "红" + colorPosfix;
    public const string unknow = "?";
    public static string GetVisibleName(CardType srcCardType, CardType dstCardType)
    {
        if (dstCardType == CardType.Invalid)
            return GetCardTypeNameEng(dstCardType) + "\n" + CardTypeHelper.GetCardTypeNameCHS(dstCardType);

        switch (srcCardType)
        {
            case CardType.Invalid:
                if (dstCardType == CardType.Invalid)
                    return GetCardTypeNameEng(dstCardType) + "\n" + CardTypeHelper.GetCardTypeNameCHS(dstCardType);
                else
                    return unknow;
            case CardType.Merlin:
                if (IsCanSee(srcCardType, dstCardType))
                    return redDes;
                else
                    return unknow;
            case CardType.Percival:
                if (IsCanSee(srcCardType, dstCardType))
                    return blueDes + "/" + redDes;
                else
                    return unknow;
            case CardType.Pengikut:
                return unknow;
            case CardType.Mordred:
                if (IsCanSee(srcCardType, dstCardType))
                    return redDes;
                else
                    return unknow;
            case CardType.Morgana:
                if (IsCanSee(srcCardType, dstCardType))
                    return redDes;
                else
                    return unknow;
            case CardType.Oberon:
                return unknow;
            case CardType.Assassin:
                if (IsCanSee(srcCardType, dstCardType))
                    return redDes;
                else
                    return unknow;
            default:
                Debug.LogError("unhanlde srcCardType:" + srcCardType);
                break;
        }
        return unknow;
    }
}
/*
 需要和这个保持一直
 public class ActionType
{
    public const int Close = 0;
    public const int Shake = 1;
    public const int Open = 2;
    public const int Glance = 3;
    public const int LookByMySelf = 4;
 */

public enum DiceType
{
    One = 1,
    Two = 2,
    Three = 3,
    Four = 4,
    Five = 5,
    Six = 6,
}